<?php

namespace app\index\controller;

use app\common\library\Measurement;
use app\index\model\Room;
use Exception;
use Redis;
use think\db\exception\DbException;
use think\facade\Cache;
use think\response\Json;

class Index extends Controller
{
    public function index(): string
    {
        return 'Hello World!';
    }

    /**
     * 获取统计指标信息
     * @return Json
     * @throws DbException
     * @noinspection PhpDynamicAsStaticMethodCallInspection
     */
    public function stat(): Json
    {
        $meas = Measurement::all();
        $totalCount = Room::count();

        return $this->renderSuccess([
            'serverTime'         => time(),
            'totalRoomCount'     => $totalCount,
            'liveScanDelay'      => $this->formatSeconds($meas[Measurement::RLSRefreshTime]['value'] ?? 0),
            'playScanDelay'      => $this->formatSeconds($meas[Measurement::RGSRefreshTime]['value'] ?? 0),
            'infoScanDelay'      => $this->formatSeconds($meas[Measurement::RRIRefreshTime]['value'] ?? 0),
            'lastLiveScanTime'   => $this->formatDate($meas[Measurement::RLSScanTime]['value'] ?? 0),
            'lastPlayScanTime'   => $this->formatDate($meas[Measurement::RGSScanTime]['value'] ?? 0),
            'lastInfoScanTime'   => $this->formatDate($meas[Measurement::RRIScanTime]['value'] ?? 0),
            'liveScanRound'      => intval($meas[Measurement::RLSScanCount]['value'] ?? 0),
            'playScanRound'      => intval($meas[Measurement::RGSScanCount]['value'] ?? 0),
            'infoScanRound'      => intval($meas[Measurement::RRIScanCount]['value'] ?? 0),
            'liveScanCount'      => intval($meas[Measurement::RLSChunkCount]['value'] ?? 0),
            'playScanCount'      => intval($meas[Measurement::RGSChunkCount]['value'] ?? 0),
            'infoScanCount'      => intval($meas[Measurement::RRIChunkCount]['value'] ?? 0),
            'liveScanRate'       => min($totalCount > 0 ? round($meas[Measurement::RLSChunkCount]['value'] / $totalCount * 100, 2) : 0, 100),
            'playScanRate'       => min($totalCount > 0 ? round($meas[Measurement::RGSChunkCount]['value'] / $totalCount * 100, 2) : 0, 100),
            'infoScanRate'       => min($totalCount > 0 ? round($meas[Measurement::RRIChunkCount]['value'] / $totalCount * 100, 2) : 0, 100),
            'liveScanEfficiency' => $totalCount > 0 ? round($meas[Measurement::RLSRefreshTime]['value'] / $totalCount, 2) : 0,
            'playScanEfficiency' => $totalCount > 0 ? round($meas[Measurement::RGSRefreshTime]['value'] / $totalCount, 2) : 0,
            'infoScanEfficiency' => $totalCount > 0 ? round($meas[Measurement::RRIRefreshTime]['value'] / $totalCount, 2) : 0,
        ]);
    }

    /**
     * 传入秒数转换为 00:00:00 的时分秒格式
     * 如果没有小时则不要显示小时的两个0
     * @param $seconds
     * @return string
     */
    private function formatSeconds($seconds): string
    {
        if ($seconds === 0) return '/';
        $hours = floor($seconds / 3600);
        $minutes = floor(($seconds - $hours * 3600) / 60);
        $seconds = $seconds - $hours * 3600 - $minutes * 60;

        return $hours > 0 ?
            sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds) :
            sprintf('%02d:%02d', $minutes, $seconds);
    }

    /**
     * 传入时间戳转换为 2020-01-01 00:00:00 的格式
     * @param $timestamp
     * @return string
     */
    private function formatDate($timestamp): string
    {
        return $timestamp > 0 ? date('Y-m-d H:i:s', $timestamp) : '/';
    }
}
